$(function(){	 
	//导航栏鼠标禁用
	getOS();
	$('#stop').css('pointer-events', 'none');	
	$("[data-toggle='tooltip']").tooltip();
	$('.previous-step').hide();
	$('.login-step').hide();
	$('#footHr').hide();
	headerEffect();
	upHeaderImg();
	choseSex();
	var i=0;
	//上一步
	$('.previous-step').click(function()
	{
		if($('#stop li:eq(1)').hasClass('active'))
		{
			PreviousFirstStep();
		}
	})
	//下一步	
	$('.next-step').click(function()
	{
		//先验证当前(第一页)表单数据是否合法
		if(checkSetUser())
		{
			if($('#stop li:first').hasClass('active'))
			{
				NextSecondStep();	
			}
		}
	})
	function NextSecondStep()
	{
		$('.previous-step').show();
		$('#stop li:first').removeClass('active');
		$('#stop li:eq(1)').addClass('active');
		$('#setUserName').removeClass('in active');
		$('#compleBasicInfo').addClass('in active');
		tipInfoPre();		
		//tipInfoNext();		
		$('.next-step').click(function()
		{
			if($('#stop li:eq(1)').hasClass('active'))
			{
				if(i==1)
				{
					$('.previous-step').show();
					$('#stop li:first').removeClass('active');
					$('#stop li:eq(1)').addClass('active');
					$('#setUserName').removeClass('in active');
					$('#compleBasicInfo').addClass('in active');
					$('.previous-step').hide();
					$('.next-step').click(function()
					{
						//先验证当前(第二页)表单数据是否合法
						if(checkCompleBasicInfo())
						{
							NextThirdStep();
						}						
					})
				}
				else
				{
					//先验证当前(第二页)表单数据是否合法
					if(checkCompleBasicInfo())
					{
						NextThirdStep();
					}
				}			
			}					
		})
	}
	function PreviousFirstStep()
	{	
		$('#stop li:eq(1)').removeClass('active');
		$('#stop li:first').addClass('active');
		$('#compleBasicInfo').removeClass('in active');
		$('#setUserName').addClass('in active');
		$('.previous-step').hide();	
		i++;
		//tipInfoNext();
		/*可能存在bug 当退回一步之后,再次下一步会直接跳到第三步*/	
	}

})
function NextThirdStep()
{	
	$('#compleBasicInfo').removeClass('in active');
	$('#stop li:eq(1)').removeClass('active');
	$('#stop li:last').addClass('active');
	$('#compleRegister').addClass('in active');
	$('.previous-step').hide();
	$('.next-step').hide();
	$('.login-step').show();	
}
//下一步提示
function tipInfoNext()
{
	if($('#stop li:eq(1)').hasClass('active'))
	{	
		$('#showInfo').click(function()
		{
			var $btn = $(this).button('loading')
		});
	}
}
//上一步提示
function tipInfoPre()
{				
	$('.previous-step').mouseover(function()
	{			
		$('#tipInfoNextPre').addClass("thumbnail list-group-item-warning");
		$('#tipInfoNextPre').text('仅允许后退一次');
		$('#tipInfoNextPre').fadeIn("slow");
	});
	$('.previous-step').mouseout(function()
	{			
		hideTipInfo();
	});		
}
//隐藏提示信息	
function hideTipInfo()
{
	$('#tipInfoNext').removeClass("thumbnail list-group-item-warning");
	$('#tipInfoNext').text('');
	$('#tipInfoNextPre').fadeOut("slow");
}
function upHeaderImg()
{
	$('.chose-header-img').on('click',function(){
		$('#imgShow').load();
	});
}
//验证用户名
function checkUserName()
{
	if($('#userNames').val()=='')
	{
		showErrorInfos(1,1);
		setTimeOuts(1);
		return false;
	}
	if($('#userNames').val().length>11)
	{
		showErrorInfos(1,2);
		setTimeOuts(1);
		return false;
	}
	return true;
}
//验证密码
function checkUserPwd()
{
	if(checkUserName())
	{
		if($('#userPwds').val()=='')
		{
			showErrorInfos(1,3);
			setTimeOuts(1);
			return false;
		}
		if($('#userPwds').val().length<3||$('#userPwds').val().length>18)
		{
			showErrorInfos(1,4);
			setTimeOuts(1);
			return false;
		}
		return true;	
	}
	else
	{
		return false;
	}		
}
//再次验证密码
function checkUserPwdAgain()
{
	if(checkUserPwd())
	{
		if($('#userPwdAgain').val()=='')
		{
			showErrorInfos(1,5);
			setTimeOuts(1);
			return false;
		}
		if($('#userPwdAgain').val()!=$('#userPwds').val())
		{
			showErrorInfos(1,6);
			setTimeOuts(1);
			return false;
		}
		return true;
	}
	else
	{
		return false;
	}		
}

//验证第一页数据
function checkSetUser()
{
	var flag_userName=checkUserName();
	if(flag_userName)
	{
		var flag_userPwd=checkUserPwd();
		if(flag_userPwd)
		{
			var flag_userPwdAgain=checkUserPwdAgain();
			if(flag_userPwdAgain)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}else
	{	
		return false;
	}	
}
//头像效果
function headerEffect()
{
	$('.header-img').mouseover(function()
	{
		$('.header-img-content').css('border','3px solid orange');
	});
	$('.header-img').mouseout(function(){
		$('.header-img-content').css('border','3px solid #CCC');
	});
}
//单选性别效果
function choseSex()
{
	$('#chose-male').click(function()
	{
		//初始化样式
		$('#chose-female').css('border','3px solid #ccc');
		$('#chose-male').css('border','3px solid #80c8f4');
		$('.chose-female-radio').val('');//初始化单选框
		//$('.chose-female-radio').removeAttr('checked');//初始化单选框选中状态s
		$('.chose-male-radio').attr('value','男');
		$('.chose-male-radio').attr("checked", true);
	});	
	$('#chose-female').click(function()
	{
		$('#chose-male').css('border','3px solid #ccc');
		$('#chose-female').css('border','3px solid #ec5791');
		$('.chose-male-radio').attr('value','');
		//$('.chose-male-radio').removeAttr('checked');
		$('.chose-female-radio').attr('value','女');
		$('.chose-female-radio').attr("checked", true);
	});
}
//由于存在bug(当选男性时无法获得其val)所以额外添加一个函数来修正$('#chose-sex').val()中的值
function getUserSex()
{
	var getMaleSex=$('.chose-male-radio').val();
	var getFemaleSex=$('.chose-female-radio').val();
	if($('.chose-male-radio').val()=='')
	{
		$('#chose-sex').val(getFemaleSex);
	}
	else if($('.chose-female-radio').val()=='')
	{
		$('#chose-sex').val(getMaleSex);
	}
}

//验证性别(是否为空)
function checkChoseSex()
{
	if($('.chose-male-radio').val()=='on'&&$('.chose-female-radio').val()=='on')
	{
		showErrorInfos(2,7);
		setTimeOuts(2);
		return false;
	}
	else
	{	
		return true;
	}
}
//邮箱验证
function checkEmail()
{
	var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/; 
	if(checkChoseSex()==false)
	{
		return false;
	}
	if($('#userEmail').val()=='')
	{
		showErrorInfos(2,8);
		setTimeOuts(2);
		return false;
	}
	if(!reg.test($('#userEmail').val()))//验证邮箱格式
	{
		showErrorInfos(2,9);
		setTimeOuts(2);
		return false;
	}
	return true;
}
//验证码
function checkValidCode()
{
	if(checkEmail()==false)
	{
		return false;
	}
	if($('#validCode').val().length!=4)
	{
		showErrorInfos(2,10);
		setTimeOuts(2);
		return false;
	}
	return true;
}
//验证第二页表单数据
function checkCompleBasicInfo()
{
	var flag_chose_sex=checkChoseSex();
	if(flag_chose_sex)
	{
		var flag_check_email=checkEmail();
		if(flag_check_email)
		{
			var flag_check_validCode=checkValidCode();
			if(flag_check_validCode)
			{
				getUserSex();
				if(upFormData())
				{
					return true;
				}
				else
				{
					return false;
				}			
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}
//延时器
function setTimeOuts(v)
{
	var selector='#Error';
	if(v==2)
	{
		selector=".errorInfo";
	}
	setTimeout(function(){
			$(selector).removeClass('alert alert-danger text-center');
			$(selector).text('');
		},3000)
}
//显示错误信息
function showErrorInfos(s,v)
{
	var selector="#Error";
	if(s==2)
	{
		selector=".errorInfo";
	}
	var errMsg='';
	switch(v)
	{
		case 1:errMsg='请填写用户名';break;
		case 2:errMsg='用户名长度超过11位';break;
		case 3:errMsg='请填写密码';break;
		case 4:errMsg='密码长度需要在3~18位之间';break;
		case 5:errMsg='请再次确认密码';break;
		case 6:errMsg='前后密码不一致';break;
		case 7:errMsg='请选择性别';break;
		case 8:errMsg='请填写邮箱';break;
		case 9:errMsg='邮箱格式错误';break;
		case 10:errMsg='请填写验证码';break;
		case 11:errMsg='用户名已被注册';break;
		case 12:errMsg='邮箱已被注册';break;
		case 13:errMsg='验证码错误';break;
	}
	$(selector).addClass('alert alert-danger text-center');
	$(selector).text(errMsg);	
}
//异步提交整个表单
function upFormData()
{
	var fm=document.getElementsByTagName('form')[1];
	var fd=new FormData(fm);
	//fd.append("userSex", $('#chose-sex').val());
	var xhr=new XMLHttpRequest();
	console.log(xhr);
	xhr.onreadystatechange=function ()
	{
		if(xhr.readyState==4)
		{
			switch(xhr.responseText)
			{
				//case '1':alert('没有选择文件');break;
				case '2':
					alert('文件类型错误');
				break;
				case '3':
					alert('请上传图像文件');
				break;
				case '4':
					alert('文件大小超出限制');
				break;
					case '5':alert('文件上传出错');
				break;
				case '1':
					$('#userNames').val('');
					$('#userPwdAgain').val('');
					$('#chose-sex').val('');
					$('#userEmail').val('');
					NextThirdStep();
				break;
				case '-1':
					showErrorInfos(2,11);
					setTimeOuts(2);
					return false;
				break;
				case '-2':
					showErrorInfos(2,12);
					setTimeOuts(2);
					return false;
				break;
				case '-3':
					showErrorInfos(2,13);
					setTimeOuts(2);
					return false;
				break;
				default:console.log(xhr.responseText);alert('未知错误,请咨询客服');
			}
			console.log(xhr.responseText);	
		}
	}
	xhr.open('POST','?c=user&a=register');
	xhr.send(fd);
	//event.preventDefault();//阻止浏览器默认动作(跳转页面)=>return false;
}
//判断浏览器(调节浏览器兼容性bug)	 
function getOS()
{
	var OsObject = "";
	if(!!window.ActiveXObject || "ActiveXObject" in window)
	{		   
	   $('#tipInfoNext').addClass('tipInfoNext-IE');
	   $('.errorInfo').addClass('errorInfo_IE');
	   $('.register-success-info').addClass('register-success-info-content-IE');
	   //return "MSIE";
	}
	else if(isFirefox=navigator.userAgent.indexOf("Firefox")!=-1)
	{
		$('#tipInfoNext').addClass('tipInfoNext');
	   // return "Firefox";
	}
	else if(isChrome=navigator.userAgent.indexOf("Chrome")!=-1)
	{		
		//调节在搜狗浏览器下因为全屏底部出现空白现象	
		$('#image-container').addClass('img-container-SouGou');
	   // return "Chrome";
	}
   
   else if(isSafari=navigator.userAgent.indexOf("Safari")!=-1) 
   {
		//return "Safari";
   }
   /* 
   else if(isOpera=navigator.userAgent.indexOf("Opera")!=-1){
		return "Opera";
   }
	*/
}
//alert("type -> "+getOs());
function setImagePreview(avalue) 
{
	var docObj = document.getElementById("upHeadImg");
	var imgObjPreview = document.getElementById("preview");
	var divs = document.getElementById("localImag");
	if (docObj.files && docObj.files[0]) 
	{
		//火狐下，直接设img属性
		imgObjPreview.style.display = 'block';
		imgObjPreview.style.width = '137px';
		imgObjPreview.style.height = '135px';
		//imgObjPreview.src = docObj.files[0].getAsDataURL();
		//火狐7以上版本不能用上面的getAsDataURL()方式获取，需要一下方式
	   imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);
	}
	else 
	{
		//IE下，使用滤镜
		docObj.select();
		var imgSrc = document.selection.createRange().text;
		var localImagId = document.getElementById("localImag");
		//必须设置初始大小
		localImagId.style.width = "137px";
		localImagId.style.height = "135px";
		//图片异常的捕捉，防止用户修改后缀来伪造图片
		try 
		{
			localImagId.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
			localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
		}
		catch(e)
		{
			alert("上传的图片格式不正确");
			return false;
		}
		imgObjPreview.style.display = 'none';
		document.selection.empty();
	}
	return true;
}